package com.ruoyi.patrol.service.impl;

import java.util.List;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.patrol.mapper.PtCheckQuestionMapper;
import com.ruoyi.patrol.domain.PtCheckQuestion;
import com.ruoyi.patrol.service.IPtCheckQuestionService;

/**
 * 专项检查问题Service业务层处理
 *
 * @author Du
 * @date 2023-11-18
 */
@Service
public class PtCheckQuestionServiceImpl implements IPtCheckQuestionService {
    @Autowired
    private PtCheckQuestionMapper ptCheckQuestionMapper;

    /**
     * 查询专项检查问题
     *
     * @param id 专项检查问题ID
     * @return 专项检查问题
     */
    @Override
    public PtCheckQuestion selectPtCheckQuestionById(Long id) {
        return ptCheckQuestionMapper.selectPtCheckQuestionById(id);
    }

    /**
     * 查询专项检查问题列表
     *
     * @param ptCheckQuestion 专项检查问题
     * @return 专项检查问题
     */
    @Override
    public List<PtCheckQuestion> selectPtCheckQuestionList(PtCheckQuestion ptCheckQuestion) {
        return ptCheckQuestionMapper.selectPtCheckQuestionList(ptCheckQuestion);
    }

    /**
     * 新增专项检查问题
     *
     * @param ptCheckQuestion 专项检查问题
     * @return 结果
     */
    @Override
    public int insertPtCheckQuestion(PtCheckQuestion ptCheckQuestion) {
        PtCheckQuestion question = ptCheckQuestionMapper.checkQuestionNameUnique(ptCheckQuestion.getQuestionName());
        if (question != null) {
            throw new CustomException("问题名称已存在!");
        }
        ptCheckQuestion.setCreateTime(DateUtils.getNowDate());
        return ptCheckQuestionMapper.insertPtCheckQuestion(ptCheckQuestion);
    }

    /**
     * 修改专项检查问题
     *
     * @param ptCheckQuestion 专项检查问题
     * @return 结果
     */
    @Override
    public int updatePtCheckQuestion(PtCheckQuestion ptCheckQuestion) {
        ptCheckQuestion.setUpdateTime(DateUtils.getNowDate());
        return ptCheckQuestionMapper.updatePtCheckQuestion(ptCheckQuestion);
    }

    /**
     * 批量删除专项检查问题
     *
     * @param ids 需要删除的专项检查问题ID
     * @return 结果
     */
    @Override
    public int deletePtCheckQuestionByIds(Long[] ids) {
        return ptCheckQuestionMapper.deletePtCheckQuestionByIds(ids);
    }

    /**
     * 删除专项检查问题信息
     *
     * @param id 专项检查问题ID
     * @return 结果
     */
    @Override
    public int deletePtCheckQuestionById(Long id) {
        return ptCheckQuestionMapper.deletePtCheckQuestionById(id);
    }

    /**
     * @param questionList
     * @param updateSupport
     * @param cId
     * @param username
     * @return 导入问题列表
     */
    @Override
    public String importQuestion(List<PtCheckQuestion> questionList, boolean updateSupport, Long cId, String username) {
        if (Collections.isEmpty(questionList)) {
            throw new CustomException("导入问题数据不能为空");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (int i = 0; i < questionList.size(); i++) {
            PtCheckQuestion question = questionList.get(i);
            question.setcId(cId);
            PtCheckQuestion uniqueQuestion = ptCheckQuestionMapper.checkQuestionNameUnique(question.getQuestionName());
            if (uniqueQuestion == null) {
                insertPtCheckQuestion(question);
                successNum++;
                successMsg.append("<br/>" + successNum + "、第 " + (i + 1) + " 条数据【导入成功】");
            } else if (updateSupport) {
                question.setUpdateBy(username);
                updatePtCheckQuestion(question);
                successNum++;
                successMsg.append("<br/>" + successNum + "、第 " + (i + 1) + " 条数据【更新成功】");
            } else {
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、第 " + (i + 1) + " 条数据【问题名称已存在】");
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "成功数据：" + successNum + " 条!<br/>" + "失败数据：共 " + failureNum + " 条，失败原因如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
